package com.dewod.producesales.activity.setting;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.dewod.producesales.MainActivity;
import com.dewod.producesales.R;
import com.dewod.producesales.adapter.SettingCenterAdapter;
import com.dewod.producesales.model.view.SettingItem;
import com.dewod.producesales.utils.SharedUtil;
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.utils.DateUtils;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SettingCenterActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private SettingCenterAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_setting_center);
        recyclerView = findViewById(R.id.setting_recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

        findViewById(R.id.view_title).setOnClickListener(v -> finish());

        initSettingItems();
    }

    private void initSettingItems() {
        // 添加配置项，后续有其他配置项，可以在这里添加
        List<SettingItem> data = new ArrayList<>();
        data.add(new SettingItem("网络", NetRequestSettingActivity.class, R.mipmap.ic_net));
        data.add(new SettingItem("工作模式", WorkModeActivity.class, R.mipmap.ic_device_model));

        // 创建适配器并设置
        adapter = new SettingCenterAdapter(data, item -> {
            if (item.getActivityClass() == null || ObjectUtils.equals(Object.class.getSimpleName(), item.getActivityClass().getSimpleName())) {
                Toast.makeText(SettingCenterActivity.this, "功能完善中", Toast.LENGTH_SHORT).show();
            } else if(ObjectUtils.equals(WorkModeActivity.class.getSimpleName(), item.getActivityClass().getSimpleName())){
                showDialogSelector();
            } else {
                Intent intent = new Intent(SettingCenterActivity.this, item.getActivityClass());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void showDialogSelector() {
        // 创建对话框
        String[] workModes = getResources().getStringArray(R.array.workMode);
        int selectedPosition = -1;

        // 获取当前已选的工作模式
        String currentMode = SharedUtil.getInstance(this).readString("WORK_MODE", workModes[0]);
        for (int i = 0; i < workModes.length; i++) {
            if (workModes[i].equals(currentMode)) {
                selectedPosition = i;
                break;
            }
        }

        // 构建 AlertDialog
        new androidx.appcompat.app.AlertDialog.Builder(this)
            .setTitle("切换工作模式")
            .setSingleChoiceItems(workModes, selectedPosition, (dialog, which) -> {
                // 保存选择
                SharedUtil.getInstance(SettingCenterActivity.this).writeString("WORK_MODE", workModes[which]);
                Toast.makeText(SettingCenterActivity.this, "已设置为：" + workModes[which], Toast.LENGTH_SHORT).show();

                Intent intent = new Intent("WORK_MODE_CHANGE");
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

                dialog.dismiss(); // 选择后关闭对话框
            })
            .show();
    }


}
